package org.jeecg.modules.coursescheduleatom.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.coursescheduleatom.entity.CouCourseScheduleAtom;
import org.jeecg.modules.coursescheduleatom.mapper.CouCourseScheduleAtomMapper;
import org.jeecg.modules.coursescheduleatom.service.ICouCourseScheduleAtomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @Description: cou_course_schedule_atom
 * @author: baochangrong
 * @Date: 2022-02-23
 * @Version: V1.0
 */
@Service
public class CouCourseScheduleAtomServiceImpl extends ServiceImpl<CouCourseScheduleAtomMapper, CouCourseScheduleAtom> implements ICouCourseScheduleAtomService {

    @Autowired
    private CouCourseScheduleAtomMapper courseScheduleAtomMapper;

    private static HashMap<String, String> classroomCache = new HashMap<>();
    private static HashMap<String, String> teacherCache = new HashMap<>();
    private static HashMap<String, String> subjectCache = new HashMap<>();

    @Override
    public List<CouCourseScheduleAtom> querylist(String scheduleId) {
        // 4 : 科目 ， 2：教室 ，3：老师
        List<CouCourseScheduleAtom> list = getScheduleListByType(scheduleId, 4);
        List<CouCourseScheduleAtom> res = new ArrayList<>(list.size());
        for (CouCourseScheduleAtom item : list) {
            CouCourseScheduleAtom weekAtom = getWeekdayObjId(item);
            res.add(weekAtom);
        }
        /**
         * 清空缓存换取内存容量./未经过jvm 测试
         */
        clearMapCache();
        return res;
    }

    private List<CouCourseScheduleAtom> getScheduleListByType(String scheduleId, Integer type) {
        QueryWrapper<CouCourseScheduleAtom> queryWrapper = new QueryWrapper<CouCourseScheduleAtom>();
        if (Objects.nonNull(scheduleId) && scheduleId.length() >= 10) {
            queryWrapper.likeRight("course_schedule_id", scheduleId);
            queryWrapper.likeRight("rel_obj_ttype", type);
        }
        return courseScheduleAtomMapper.selectList(queryWrapper);
    }

    /**
     * 只对当前的课表有用，防止OOM
     * <p>
     * 考虑多线程
     */
    private void clearMapCache() {
        classroomCache.clear();
        teacherCache.clear();
        subjectCache.clear();
    }

    @Override
    public List<CouCourseScheduleAtom> querylist() {
        return querylist(null);
    }

    private CouCourseScheduleAtom getWeekdayObjId(CouCourseScheduleAtom atom) {
        String weekdayId = "";
        Integer objType = atom.getRelObjTtype();
        for (int i = 1; i <= 7; i++) {
            switch (i) {
                case 1:
                    weekdayId = atom.getCourScheAtomMonday();
                    atom.setCourScheAtomMonday(getSubjectObjId(weekdayId, objType));
                    break;
                case 2:
                    weekdayId = atom.getCourScheAtomTuesday();
                    atom.setCourScheAtomTuesday(getSubjectObjId(weekdayId, objType));
                    break;
                case 3:
                    weekdayId = atom.getCourScheAtomWednesday();
                    atom.setCourScheAtomWednesday(getSubjectObjId(weekdayId, objType));
                    break;
                case 4:
                    weekdayId = atom.getCourScheAtomThursday();
                    atom.setCourScheAtomThursday(getSubjectObjId(weekdayId, objType));
                    break;
                case 5:
                    weekdayId = atom.getCourScheAtomFriday();
                    atom.setCourScheAtomFriday(getSubjectObjId(weekdayId, objType));
                    break;
                case 6:
                    weekdayId = atom.getCourScheAtomSaturday();
                    atom.setCourScheAtomSaturday(getSubjectObjId(weekdayId, objType));
                    break;
                case 7:
                    weekdayId = atom.getCourScheAtomSunday();
                    atom.setCourScheAtomSunday(getSubjectObjId(weekdayId, objType));
                    break;
            }
        }
        return atom;
    }

    private String getSubjectObjId(String dictId, Integer objType) {
        return dictId.length() >= 4 && objType == 4 ?
                getByObjName(objType, dictId) :
                courseScheduleAtomMapper.queryDictTextByDictValue(dictId);
    }

    /**
     * 缓存处理
     * 减少SqlServer
     *
     * @param type
     * @param objId
     * @return
     */
    private String getByObjName(Integer type, String objId) {
        String objName = "";
        switch (type) {
            case 2:
                objName = classroomCache.containsKey(objId) ? classroomCache.get(objId) : queryObjName(type, objId);
                break;
            case 3:
                objName = teacherCache.containsKey(objId) ? teacherCache.get(objId) : queryObjName(type, objId);
                break;
            case 4:
                objName = subjectCache.containsKey(objId) ? subjectCache.get(objId) : queryObjName(type, objId);
                break;
            default:
                log.error("无法找到类型映射");
        }
        return objName;
    }


    private String queryObjName(Integer type, String objId) {
        String objName = courseScheduleAtomMapper.queryObjectNameByObjectId(type, objId);
        switch (type) {
            case 2:
                classroomCache.put(objId, objName);
                break;
            case 3:
                teacherCache.put(objId, objName);
                break;
            case 4:
                subjectCache.put(objId, objName);
                break;
            default:
                log.error("为从数据库中查询到对项名");
        }
        return objName;
    }


}
